<template>
	<view class="content">
		<view class="wrap">
			<view class="title">文本检测V2</view>
			<uni-easyinput v-model="text" placeholder="请输入检测内容" type="textarea" :maxlength="1000000"></uni-easyinput>
			<view class="tips">
				违规词示例：习主、草泥马、成网人站
			</view>
			<button type="primary" class="check-btn" @click="textSecCheck">检测</button>
		</view>
		<view class="wrap">
			<view class="title">图片检测V2（异步回调）</view>
			<view class="desc">支持图片格式包括 jpg, jepg, png, bmp, gif（取首帧）</view>
			<uni-file-picker @success="fileUploadedV2" title="请上传待检测的图片" limit="1" file-mediatype="image"></uni-file-picker>
			<!-- <button type="primary" class="check-btn" @click="checkImageV2">检测</button> -->
		</view>
		<view class="wrap">
			<view class="title">音视频检测V2（异步回调）</view>
			<view class="desc">支持的音频格式包括mp3, aac, ac3, wma, flac, vorbis, opus, wav</view>
			<uni-easyinput v-model="avUrl" placeholder="请输入音视频地址"></uni-easyinput>
			<button type="primary" class="check-btn" @click="checkAv">检测</button>
		</view>
		<view class="wrap">
			<view class="title">图片检测v1</view>
			<uni-file-picker @success="fileUploadedV1" title="请上传1M以内的图片" limit="1" file-mediatype="image"></uni-file-picker>
			<view class="tips">
				由于v1版本微信暂停更新，目前v1的检测能力很弱
			</view>
		</view>
	</view>
</template>

<script>
	const testUniSecCheck = uniCloud.importObject("test-uni-sec-check");
	export default {
		data() {
			return {
				openid: '',
				text: '',
				avUrl: ''
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			const info = uni.getAccountInfoSync();
			let appid = info.miniProgram.appId;
			let openid = uni.getStorageSync(`uni-sec-check-openid-${appid}`);
			if (openid) {
				this.openid = openid;
			} else {
				uni.login({
					provider: "weixin",
					success: async (res) => {
						let getOpenidRes = await testUniSecCheck.getOpenid({
							code: res.code
						});
						this.openid = getOpenidRes.openid;
						uni.setStorageSync(`uni-sec-check-openid-${appid}`, getOpenidRes.openid);
					}
				});
			}
			// #endif
		},
		methods: {
			// 检测文本
			async textSecCheck() {
				if (!this.text) {
					uni.showModal({
						content: `请输入待检测的文本`,
						showCancel: false
					});
					return;
				}
				let res = await testUniSecCheck.textSecCheck({
					content: this.text,
					openid: this.openid,
					scene: 2,
					version: 2, // 固定为2
				});

				if (res.errCode) {
					uni.showModal({
						title: res.errMsg,
						content: `涉嫌 ${res.result.label}`,
						showCancel: false
					})
				}
				if (res.errCode === 0) {
					uni.showModal({
						title: '检测结果',
						content: '通过',
						showCancel: false
					})
				}
			},
			fileUploadedV1(res) {
				this.checkImageV1(res.tempFiles[0].url);
			},
			// v1版本检测能力很弱，建议使用v2
			async checkImageV1(url) {
				if (!url || url.indexOf("http") !== 0) {
					uni.showModal({
						content: `请上传待检测的图片`,
						showCancel: false
					});
					return;
				}
				let res = await testUniSecCheck.imgSecCheck({
					image: url,
					openid: this.openid,
					scene: 2,
					version: 1,
				});
				if (res.errCode) {
					uni.showModal({
						title: res.errMsg,
						content: `涉嫌 ${res.result?.label ?? '风险'}`,
						showCancel: false
					})
				}
				if (res.errCode === 0) {
					uni.showModal({
						title: '检测结果',
						content: '通过',
						showCancel: false
					})
				}
			},
			fileUploadedV2(res) {
				this.checkImageV2(res.tempFiles[0].url);
			},
			// 检测图片v2版本
			async checkImageV2(url) {
				if (!url || url.indexOf("http") !== 0) {
					uni.showModal({
						content: `请上传待检测的图片`,
						showCancel: false
					});
					return;
				}
				let res = await testUniSecCheck.imgSecCheck({
					image: url,
					openid: this.openid,
					scene: 2,
					version: 2,
				});
				if (res.errCode) {
					uni.showModal({
						content: res.errMsg,
						showCancel: false
					})
				} else if (res.errCode === 0) {
					uni.showModal({
						content: `traceId：${res.traceId}，微信会在5-30分钟后回调你设置的异步回调地址（目前异步回调逻辑需要自己实现）`,
						showCancel: false
					})
				}
			},
			// 检测音频
			async checkAv() {
				let url = this.avUrl;
				if (!url || url.indexOf("http") !== 0) {
					uni.showModal({
						content: `请输入正确的音频URL`,
						showCancel: false
					});
					return;
				}
				let res = await testUniSecCheck.avSecCheck({
					mediaUrl: url,
					openid: this.openid,
					scene: 2,
					version: 2,
				});
				if (res.errCode) {
					uni.showModal({
						content: res.errMsg,
						showCancel: false
					})
				} else if (res.errCode === 0) {
					uni.showModal({
						content: `traceId: ${res.traceId}，微信会在5-30分钟后回调你设置的异步回调地址（目前异步回调逻辑需要自己实现）`,
						showCancel: false
					})
				}
			}
		}
	}
</script>

<style>
	.content {
		padding: 15px;
	}

	.wrap {
		padding: 15px 0;
		border-bottom: #f6f6f6 solid 1px;
	}

	.wrap:last-child {
		border-bottom: none;
	}

	.title {
		font-size: 16px;
		margin-bottom: 10px;
		font-weight: bold;
	}

	.desc {
		font-size: 13px;
		color: #666;
		margin-top: 5px;
		margin-bottom: 10px;
	}

	.check-btn {
		margin-top: 10px;
	}

	.tips {
		margin-top: 10px;
		font-size: 12px;
		color: red;
	}
</style>